package com.tingyukeji.fastpay.shop.activity;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.tingyukeji.fastpay.R;
import com.tingyukeji.fastpay.base.BaseActivity;
import com.tingyukeji.fastpay.my.activity.BankListActivity;
import com.tingyukeji.fastpay.collection.activity.CollectionBillActivity;
import com.tingyukeji.fastpay.databinding.ShopDetailActivityBinding;
import com.tingyukeji.fastpay.view.ZQImageViewRoundOval;
import com.tingyukeji.fastpay.view.bannerview.MZBannerView;
import com.tingyukeji.fastpay.view.bannerview.holder.MZHolderCreator;
import com.tingyukeji.fastpay.view.bannerview.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/11.
 */

public class ShopDetailActivity extends BaseActivity implements View.OnClickListener {
    private ShopDetailActivityBinding binding;
    public static final int[] BANNER = new int[]{R.mipmap.banner1, R.mipmap.banner2, R.mipmap.banner3, R.mipmap.banner4, R.mipmap.banner5};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        super.setContentView();
        binding = DataBindingUtil.setContentView(this, R.layout.shop_detail_activity);
    }

    @Override
    protected void initView() {
        super.initView();
        initBanner();
    }

    private void initBanner() {
        binding.shopBanner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int position) {
                Toast.makeText(context, "click page:" + position, Toast.LENGTH_LONG).show();
            }
        });
        binding.shopBanner.addPageChangeLisnter(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.e("", "----->addPageChangeLisnter:" + position + "positionOffset:" + positionOffset + "positionOffsetPixels:" + positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                Log.e("", "addPageChangeLisnter:" + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        List<Integer> bannerList = new ArrayList<>();
        for (int i = 0; i < BANNER.length; i++) {
            bannerList.add(BANNER[i]);
        }
        binding.shopBanner.setIndicatorVisible(true);

        binding.shopBanner.setPages(bannerList, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
    }

    public static class BannerViewHolder implements MZViewHolder<Integer> {
        private ZQImageViewRoundOval mImageView;

        @Override
        public View createView(Context context) {
            // 返回页面布局文件
            View view = LayoutInflater.from(context).inflate(R.layout.shop_banner_item, null);
            mImageView = (ZQImageViewRoundOval) view.findViewById(R.id.banner_image);
            mImageView.setType(ZQImageViewRoundOval.TYPE_ROUND);
//            mImageView.setRoundRadius(30);
            return view;
        }

        @Override
        public void onBind(Context context, int position, Integer data) {
            // 数据绑定
            mImageView.setImageResource(data);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        binding.shopBanner.pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        binding.shopBanner.start();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ic_bill:
                goActivity(CollectionBillActivity.class);
                break;
            case R.id.rl_select_bank:
                goActivity(BankListActivity.class);
                break;
        }
    }

}
